// components/home-statistics/index.js
import {
  request
} from '../../api/index'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    dataSource: [],
    loading: false,
    actived: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    fetchData() {
      const _this = this;
      return new Promise((resove,reject)=>{
        request.materialStatistics({}, function (res) {
          const _all = {
            status: '',
            number: res.reduce((curentVal, preVal) => {
              return curentVal + Number(preVal.number);
            }, 0)
          }
          res.unshift(_all);
          _this.setData({
            dataSource: res.filter(it=> it.status !== 'CHECK')
          });
          
          resove();
        }, reject)
      }).catch(e=>{
        this.setData({
          loading: false
        })
      })

    },
    ontap(event) {
      const status = event.currentTarget.dataset.status;
      this.setData({
        actived: status
      })
      this.triggerEvent('click',status);
    },
    /**
     * Loading
     */
    async onLoading() {
      this.setData({
        loading: true
      })
      await this.fetchData();
      this.setData({
        loading: false
      })
    }
  },
  lifetimes: {
    attached() {
      // 在组件实例进入页面节点树时执行
      this.fetchData();
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },

  }
})